var tblCourse;
$(function () {
    tblCourse = $('#tbl-course').DataTable(buildCourseOptions());
    trCssEvent(tblCourse);
});

function buildCourseOptions() {
    var options = {
        "autoWidth": false,
        "deferRender": true,
        "searching": false,
        "ordering": true,
        "processing": true,
        "scrollX": true,
        "serverSide": true,
        "language": {
            "url": "//cdn.datatables.net/plug-ins/1.10.12/i18n/Chinese.json"
        },
        "ajax": {
            "url": '/api/course',
            "type": 'POST',
            "dataType": 'json',
            data: function (params) {
                params['childId'] = $("#childId").val();
                params['courseId'] = $("#courseId").val();
            }
        },
        "columns": [{
            "data": "CourseName"
        }, {
            "data": "TeacherName"
        }, {
            "data": "Date"
        }, {
            "data": "WeekStr"
        }, {
            "data": "CourseTime"
        }, {
            "data": "Status"
        }, {
            "data": "Score"
        }],
        columnDefs: [{"orderable": false, "targets": [0, 1, 3, 4, 5, 6]}
        ], "order": [[2, 'desc']],
        "dom": "<'row'<'col-xs-12't>>r<'row'<'col-xs-2 lstyle'l><'col-xs-3'i><'col-xs-6  fright'p>>"
    };
    return options;
}


// 查看孩子配置
function selectCodeConfig(v) {
    var code = $("#code").val();
    var childId = $("#childId").val();
    var codename = $("#code").find("option:selected").text();
    $.ajax({
        url: "api/config",
        type: "post",
        data: {code: code, childId: childId},
        success: function (data) {
            var configs = data;
            var conf ='没有配置信息';
            if (configs!=null&&configs.length > 0) {
                for (var i = 0; i < configs.length; i++) {
                    if (i==0){
                        conf = getConfigStr(configs[i].Name, configs[i].Value); // 转化name，和value 为展示的字符串
                    }else {
                        conf += getConfigStr(configs[i].Name, configs[i].Value); // 转化name，和value 为展示的字符串
                    }
                }

            }
            configsAlert(codename, conf);
        }
    });
}

function configsAlert(codeName, conf) {
    $.alert({
        backgroundDismiss: true,
        type: 'black',
        title: codeName,
        content: conf
    });
}

function getConfigStr(name, value) {
    return  '<div style="width: 100%;"><div style="display: inline-block;width: 60%;"><b>'+name+':</b></div><div style="display: inline-block;width: 40%;">'+valueStr+'</div></div>';
}
